package com.easy.system.util;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import com.easy.yours.biz.model.Clazz;
import com.easy.yours.biz.model.Demo;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-7-5
 * Time: 下午3:54
 * To change this template use File | Settings | File Templates.
 */
public class ReflectUtil<T> {
    private Class<T> clazz;

    public Class<T> getClazz() {
        return clazz;
    }

    public void setClazz(Class<T> clazz) {
        this.clazz = clazz;
    }

    public ReflectUtil() {
        this.clazz = (Class<T>) getSuperClassGenricType(getClass(), 0);
    }

    public static Class<Object> getSuperClassGenricType(final Class clazz, final int index) {

        //返回表示此 Class 所表示的实体（类、接口、基本类型或 void）的直接超类的 Type。
        Type genType = clazz.getGenericSuperclass();

        if (!(genType instanceof ParameterizedType)) {
            return Object.class;
        }
        //返回表示此类型实际类型参数的 Type 对象的数组。
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

        if (index >= params.length || index < 0) {
            return Object.class;
        }
        if (!(params[index] instanceof Class)) {
            return Object.class;
        }
        return (Class) params[index];
    }

    /**
     * 得到属性的操作
     *
     * @param clazz
     * @return
     * @throws Exception
     */
    public static String getFields(Class clazz) throws Exception {
//        Class userClass = Class.forName(obj.toString().split("@")[0]);//加载类
        Field[] fields = clazz.getDeclaredFields();//获得对象方法集合
        StringBuilder stringBuilder = new StringBuilder();
        //暂时不需要去父类的属性
       /* Class superClazz = clazz.getSuperclass();
        while (true) {
            if (superClazz.getSimpleName().equals(Object.class.getSimpleName())) {
                break;
            }
            Field[] superFields = superClazz.getDeclaredFields();
            for (Field superField : superFields) {
                if (!superField.getName().equals("id")) {
                    stringBuilder.append(superField.getName());
                    stringBuilder.append(",");
                }
            }
            superClazz = superClazz.getSuperclass();
        }*/
        for (Field field : fields) {
            //过滤集合属性
            if(!(field.getGenericType() instanceof ParameterizedType || field.getName().equals("id"))){
                stringBuilder.append(field.getName());
                stringBuilder.append(",");
            }
        }
        stringBuilder.setLength(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }

    /**
     * 获得属性类型键值对
     *
     * @param clazz
     * @return
     */
    public static Map<String, String> getFieldTypeMap(Class clazz) {
        Map<String, String> map = new HashMap();
        Field[] fields = clazz.getDeclaredFields();//获得对象方法集合
        Class superClazz = clazz.getSuperclass();
        while (true) {
            if (superClazz.getSimpleName().equals(Object.class.getSimpleName())) {
                break;
            }
            Field[] superFields = superClazz.getDeclaredFields();
            for (Field superField : superFields) {
                map.put(superField.getName(), superField.getGenericType().toString());
            }
            superClazz = superClazz.getSuperclass();
        }
        for (Field field : fields) {
            map.put(field.getName(), field.getGenericType().toString());
        }
        return map;
    }


/*    *//**
     *
     * @throws Exception
     *//*
    public static Object setObjectFieldValue(Class clazz){

    }*/
    public static void main(String[] args) throws Exception {
//        System.out.println(getFields(Demo.class));
        getFields(Clazz.class) ;
    }
}
